package com.demo2do.fms.admin.web.controller.supplier.sub.info;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.supplier.VehicleSourceReportSearch;
import com.demo2do.fms.admin.entity.search.supplier.VehicleSourceSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.service.supplier.VehicleSourceService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.Type;
import com.demo2do.fms.entity.vehicle.source.VehicleSource;

/**
 * 分属供方车辆信息查询
 * 
 * 业务逻辑：
 *    查询分属供方车辆信息
 *        - 总部：可看所有车辆信息
 *        - 分区：可看所有车辆信息
 *        - 一级供方：可看所有属于该一级供方的二级供方的车辆信息 （如果要看一级供方自己的车辆信息，请至驳运车信息查询）
 *        - 二级供方： 无可看
 */
@Controller
@RequestMapping("/sub-vehicles")
@MenuSetting("menu-supplier")
public class SubVehicleController {

    @Autowired
    private VehicleSourceService vehicleSourceService;
    
    @Autowired
    private SupplierService supplierService;
    
    /**
     * 车辆列表
     *
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-sub-vehicle-management")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/vehicle/sub-vehicle-list");
        
        int totals = vehicleSourceService.countSub(login);
        if (totals > 0) {
            
            List<VehicleSource> vehicleSources = vehicleSourceService.listSub(login, page.with(totals));
            
            modelAndView.addObject("vehicleSources", vehicleSources);
            modelAndView.addObject("page", page);
            
        }

        return modelAndView;
        
    }

    /**
     * search 车辆信息查询
     * 
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-sub-vehicle-management")
    public ModelAndView search(@Secure Login login, Page page) {
        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/vehicle/sub-vehicle-search");
        return modelAndView;
    }
    
    /**
     * on search
     * 
     * @param login
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-sub-vehicle-management")
    public ModelAndView onSearch(@Secure Login login, VehicleSourceSearch search,
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/vehicle/sub-vehicle-list");

        int totals = vehicleSourceService.countSub(search, login);
        if (totals > 0) {
            
            List<VehicleSource> vehicleSources = vehicleSourceService.searchSub(search, login, page.with(totals));
            modelAndView.addObject("vehicleSources", vehicleSources);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 导出所属车辆
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/vehicle/sub-vehicle-export");
        
        List<Supplier> suppliers = supplierService.listSubSupplier(login, Type.MOTOR);
        modelAndView.addObject("suppliers", suppliers);
        
        return modelAndView;
        
    }
    
    /**
     * 导出所属车辆
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public ModelAndView onExport(VehicleSourceReportSearch search) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("supplier/vehicle-info-export.xlsx"));
        
        // 1. list result
        List<Map<String, Object>> result = vehicleSourceService.export(search);
        modelAndView.addObject("result", result);
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}
